package com.qf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsUserAccountMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/15 17:42
 */
@Service
public class DtsUserAccountServiceImpl implements DtsUserAccountService {

    @Autowired
    DtsUserAccountMapper userAccountMapper;
    @Autowired
    DtsUserMapper userMapper;
    @Override
    public void updateRateByUserId(int userId, int settlementRate) {
//        用useid查找一个DtsUserAccount
        LambdaQueryWrapper<DtsUserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(DtsUserAccount::getUserId,userId);

        DtsUserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);

//        if (null == userAccount ){
//            userAccount = new DtsUserAccount();
//            userAccount.setUserId(userId);
//            userAccount.setCreateTime(new Date());
//            userAccount.setSettlementRate(settlementRate);
//            userAccount.setModifyTime(new Date());
//            userAccount.setStatus(1);
//            userAccount.setRemainAmount(new BigDecimal(0));
//            userAccount.setTotalAmount(new BigDecimal(0));
//
//
//        }
//        更新操作
        if (settlementRate!=0){
            userAccount.setSettlementRate(settlementRate);
        }
        userAccountMapper.updateById(userAccount);

        DtsUser user = userMapper.selectById(userId);
        user.setUserLevel(2);
        user.setLastLoginTime(new Date());
        user.setStatus(0);
        userMapper.updateById(user);
    }

    @Override
    public DtsUserAccount selectById(Integer useid) {
        LambdaQueryWrapper<DtsUserAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsUserAccount::getUserId,useid);
        DtsUserAccount userAccount = userAccountMapper.selectOne(wrapper);
        return userAccount;
    }
}
